package dao;

import model.Food;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;

public class FoodDAO implements IFoodDAO {
	private JdbcTemplate jdbcTemplate;

	public FoodDAO(DataSource dataSource) {
		jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public List<Food> getList() {
		String sql = "SELECT * FROM cnpm.food;";
		List<Food> listContact = jdbcTemplate.query(sql, new RowMapper<Food>() {

			public Food mapRow(ResultSet rs, int rowNum) throws SQLException {
				Food food = new Food();
				food.setName(rs.getString("name"));
				food.setType(rs.getString("type"));
				food.setSource(rs.getString("source"));
				food.setDateOfPurchase(rs.getDate("dateOfPurchase"));
				food.setWholesalePrice(rs.getDouble("wholesalePrices"));
				food.setRetailPrice(rs.getDouble("retailPrice"));
				food.setStatus(rs.getString("status"));
				food.setInfo(rs.getString("info"));
				food.setImage(rs.getString("image"));
				food.setQr(rs.getString("qr"));
				return food;
			}

		});

		return listContact;
	}

	public void saveOrUpdate(Food food) {
		if (food.getId() > 0) {
			String sql = "UPDATE cnpm.food SET name=?, type=?, source=?, dateOfPurchase=?, "
					+ "wholesalePrices=?, retailPrice=?, status=?, info=?, image=?, qr=? WHERE id=?";
			jdbcTemplate.update(sql, food.getName(), food.getType(),
					food.getSource(), food.getDateOfPurchase(),
					food.getWholesalePrice(), food.getRetailPrice(),
					food.getStatus(), food.getInfo(), food.getImage(), food.getQr(),
					food.getId());
		} else {
			String sql = "INSERT INTO cnpm.food (name, type, source, dateOfPurchase, wholesalePrices, retailPrice, status, info, image, qr)"
					+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
			jdbcTemplate.update(sql, food.getName(), food.getType(),
					food.getSource(), food.getDateOfPurchase(),
					food.getWholesalePrice(), food.getRetailPrice(),
					food.getStatus(), food.getInfo(), food.getImage(), food.getQr());
		}
	}

	public <T> Food getFood(String object, T t) {
		String sql = "SELECT * FROM cnpm.food WHERE "+object+"=" + t;
		return jdbcTemplate.query(sql, new ResultSetExtractor<Food>() {

			public Food extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				if (rs.next()) {
					Food food = new Food();
					food.setName(rs.getString("name"));
					food.setType(rs.getString("type"));
					food.setSource(rs.getString("source"));
					food.setDateOfPurchase(rs.getDate("dateOfPurchase"));
					food.setWholesalePrice(rs.getDouble("wholesalePrices"));
					food.setRetailPrice(rs.getDouble("retailPrice"));
					food.setStatus(rs.getString("status"));
					food.setInfo(rs.getString("info"));
					food.setImage(rs.getString("image"));
					food.setQr(rs.getString("qr"));
					return food;
				}

				return null;
			}

		});
	}

	public void deleteFood(int id) {
		String sql = "DELETE FROM cnpm.food WHERE id=?";
		jdbcTemplate.update(sql, id);

	}

}